package com.xcy.prime;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    private EditText inputNumber;
    private Button checkButton;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inputNumber = findViewById(R.id.input_number);
        checkButton = findViewById(R.id.check_button);

        checkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = inputNumber.getText().toString();
                switch (input.toString()) {
                    case "521":
                        intent = new Intent(MainActivity.this, SecondActivity.class);
                        startActivity(intent);
                        break; //可选
                    case "新年快乐":
                        intent = new Intent(MainActivity.this, NewYearActivity.class);
                        startActivity(intent);
                        break; //可选
                    case "情人节快乐":
                        intent = new Intent(MainActivity.this, ValentineActivity.class);
                        startActivity(intent);
                        break; //可选
                    default: //可选

                        String text = input.toString(); // 假设文本为这个字符串
                        String prefix = "#";
                        String compareText = "29107040706yes";

                        // 检查文本是否以 # 开头且长度为 14
                        if (text.startsWith(prefix) && text.length() == 15) {
                            String subText = text.substring(1); // 去除 # 号
                            if (subText.equals(compareText)) {
                                Intent textIntent = new Intent(Intent.ACTION_SEND);
                                textIntent.setType("text/plain");
                                textIntent.putExtra(Intent.EXTRA_TEXT, "开学出来呀我亲爱的老婆大人");
                                startActivity(Intent.createChooser(textIntent, "请打开支付宝口令红包！！！！"));
                                Toast.makeText(MainActivity.this, "分享给任意好友就能看到口令内容啦！", Toast.LENGTH_SHORT).show();

                            } else {
                                int matchCount = 0;
                                for (int i = 0; i < subText.length(); i++) {
                                    if (subText.charAt(i) == compareText.charAt(i)) {
                                        matchCount++;
                                    }
                                }
                                double accuracy = (double) matchCount / subText.length()*100;
                                // 格式化正确率
                                DecimalFormat df = new DecimalFormat("#00.00");
                                String formattedAccuracy = df.format(accuracy);
                                Toast.makeText(MainActivity.this, "正确率：" + formattedAccuracy, Toast.LENGTH_SHORT).show();

                            }
                        } else {
                            Toast.makeText(MainActivity.this, "格式错误！", Toast.LENGTH_SHORT).show();
                        }


                }
            }
        });
    }
}
